﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Design;

namespace _0812367_0912581
{
    public class EnemySprite: MySprite
    {
        private Vector2 _speed;

        public Vector2 Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }

        private bool isLiving = true;

        public bool IsLiving
        {
            get { return isLiving; }
            set { isLiving = value; }
        }
        private int hp;

        public int Hp
        {
            get { return hp; }
            set { hp = value; }
        }

        private bool allowFire;

        public bool AllowFire
        {
            get { return allowFire; }
            set { allowFire = value; }
        }
        //private int time


        private Texture2D bulletTexture;

        public Texture2D BulletTexture
        {
            get { return bulletTexture; }
            set { bulletTexture = value; }
        }

        private int bulletSpeed = 10;

        public int BulletSpeed
        {
            get { return bulletSpeed; }
            set { bulletSpeed = value; }
        }


        private int monterSpeed = 1;

        public int MonterSpeed
        {
            get { return monterSpeed; }
            set { monterSpeed = value; }
        }

        private int timeSinceLastFire = 0;

        public int TimeSinceLastFire
        {
            get { return timeSinceLastFire; }
            set { timeSinceLastFire = value; }
        }
        private int timeFireDelay;

        public int TimeFireDelay
        {
            get { return timeFireDelay; }
            set { timeFireDelay = value; }
        }

        private List<EnermyBullet> monterBullets = new List<EnermyBullet>();

        public List<EnermyBullet> MonterBullets
        {
            get { return monterBullets; }
            set { monterBullets = value; }
        }

        public EnemySprite(Texture2D aImage, Point aSheetsize, Vector2 thePosition, int theColOffset, float scale, int timePerFrame, Vector2 theSpeed, bool isLiving, Texture2D aBulletTexture, int theHp, bool AllowFire, int TimeFireDelay)
            : base(aImage, aSheetsize, thePosition, theColOffset, scale, timePerFrame)
        {
            this.Speed = theSpeed;
            this.isLiving = isLiving;
            this.bulletTexture = aBulletTexture;//********
            this.hp = theHp;
            this.allowFire = AllowFire;
            this.timeFireDelay = TimeFireDelay;
        }

        public EnemySprite(EnemySprite aMonter)
            : base(aMonter)
        {
            this.Speed = aMonter.Speed;
            this.isLiving = aMonter.isLiving;
            this.bulletTexture = aMonter.bulletTexture;
            this.hp = aMonter.hp;
            this.allowFire = aMonter.allowFire;
            this.timeFireDelay = aMonter.timeFireDelay;
        }


        //public virtual void LoadContent(ContentManager contentMan)
        //{
        //    //base.LoadContent(contentMan);
        //    this.Content = contentMan;

        //    foreach (MonterBullet bullet in monterBullets)
        //    {
        //        bullet.LoadContent(this.Content);
        //    }

        //}


        public virtual void Update(GameTime gameTime)
        {            
            Move(gameTime);
            Fire(gameTime);
            UpdateHp(gameTime);
            base.Update(gameTime);
        }

        public override void  Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
 	        base.Draw(gameTime, spriteBatch);

            foreach (EnermyBullet bullet in monterBullets)
            {
                if (bullet.Visible)
                {
                    bullet.Draw(gameTime, spriteBatch);
                }
            }
        }

        //public virtual 

        public virtual void UpdateHp(GameTime gameTime)
        {
            if (this.hp <= 0)
                this.isLiving = false;
        }

        public virtual void Move(GameTime gameTime)
        {
            //Vector2 elapsedTime = new Vector2((float)gameTime.ElapsedGameTime.Seconds, 1.0f);
            Position = (Position - Speed);// *elapsedTime;


        }

        public virtual void Fire(GameTime gameTime)
        {
            if (this.allowFire)
            {
                if (isLiving)
                {
                    ShootAbullet(gameTime);

                    foreach (EnermyBullet bullet in monterBullets)
                    {
                        bullet.UpdateVisible(gameTime);

                    }

                    //int n = monterBullets.Count;
                    for (int i = 0; i < monterBullets.Count; i++)
                    {
                        if (monterBullets[i].Visible == false)
                        {
                            monterBullets.RemoveAt(i);
                        }
                    }

                    foreach (EnermyBullet bullet in monterBullets)
                    {
                        bullet.Update(gameTime);
                    }
                }
            }
        }

        public virtual void ShootAbullet(GameTime gameTime)
        {
            timeSinceLastFire += gameTime.ElapsedGameTime.Milliseconds;

            //float x0 = this.FrameSize.X * Scale / 2;
            float y0 = this.FrameSize.Y * Scale / 2 - this.CollisionOffset;
            if (timeSinceLastFire > timeFireDelay)
            {
                timeSinceLastFire = 0;

                EnermyBullet bullet = new EnermyBullet(bulletTexture, new Point(1, 1), this.Position + new Vector2(0, y0), 20, 0.3f, 20, new Vector2(bulletSpeed, 0), true);
                monterBullets.Add(bullet);
            }
        }

        public override Rectangle collisionRect
        {
            get
            {
                return new Rectangle((int)(this.Position.X + this.CollisionOffset * this.Scale * 2), (int)(this.Position.Y), this.FrameSize.X, this.FrameSize.Y);
            }
        }
    }
}
